package com.zust.zst.chooselesson.service.impl;

import com.zust.zst.chooselesson.service.CourseInfoManagementService;
import com.zust.zst.common.dto.chooselesson.AddCourseDTO;
import com.zust.zst.common.dto.chooselesson.GetCourseByTermAndCollegeDTO;
import com.zust.zst.common.dto.chooselesson.SearchCourseDTO;
import com.zust.zst.common.vo.chooselesson.CourseInfoVO;
import com.zust.zst.dao.mapper.chooselesson.CourseInfoManagementMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程信息管理服务层实现类
 *
 * @author Gavin Zhang
 * @date 2022/11/22 20:45
 */

@Service("courseInfoManagementServiceImpl")
public class CourseInfoManagementServiceImpl implements CourseInfoManagementService {

    @Resource
    private CourseInfoManagementMapper courseInfoManagementMapper;

    @Override
    public List<String> getAllTerm() {
        return courseInfoManagementMapper.getAllTerm();
    }

    @Override
    public List<String> getAllColleges() {
        return courseInfoManagementMapper.getAllColleges();
    }

    @Override
    public int addCourse(AddCourseDTO addCourseDTO) {

        int collegeId = courseInfoManagementMapper.getCollegeIdByName(addCourseDTO.getCollege());
        return courseInfoManagementMapper.addCourse(collegeId, addCourseDTO);
    }

    @Override
    public List<CourseInfoVO> getCourseByTermAndCollege(GetCourseByTermAndCollegeDTO dto) {
        // 获取collegeId
        int collegeId = courseInfoManagementMapper.getCollegeIdByName(dto.getCollege());
        int page = (dto.getPage() - 1) * dto.getPageSize();
        dto.setPage(page);
        return courseInfoManagementMapper.getCourseByTermAndCollege(collegeId, dto);
    }

    @Override
    public int getTotalLineByTermAndCollege(String term, String college) {
        int collegeId = courseInfoManagementMapper.getCollegeIdByName(college);
        return courseInfoManagementMapper.getTotalLineByTermAndCollege(term, collegeId);
    }

    @Override
    public List<CourseInfoVO> getCoursesBySearchingName(SearchCourseDTO searchCourseDTO) {
        int collegeId = courseInfoManagementMapper.getCollegeIdByName(searchCourseDTO.getCollege());
        int page = (searchCourseDTO.getPage() - 1) * searchCourseDTO.getPageSize();
        searchCourseDTO.setPage(page);
        return courseInfoManagementMapper.getCoursesBySearchingName(collegeId, searchCourseDTO);
    }

    @Override
    public int getTotalLineBySearchingCourse(String term, String college, String course) {
        int collegeId = courseInfoManagementMapper.getCollegeIdByName(college);
        return courseInfoManagementMapper.getTotalLineBySearchingCourse(term, collegeId, course);
    }

    @Override
    public int updateCourse(CourseInfoVO courseInfoVO) {
        return courseInfoManagementMapper.updateCourse(courseInfoVO);
    }

    @Override
    public int deleteCourseById(int courseId) {
        return courseInfoManagementMapper.deleteCourseById(courseId);
    }
}
